﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmStockSearch : BaseForm
    {
        public FrmStockSearch()
        {
            InitializeComponent();
        }

        private void InitDictItem()
        {
            this.txtSearchWareHouse.Items.Clear();
            this.txtSearchWareHouse.Items.AddRange(DictItemUtil.GetWareHouse(true));
            this.txtSearchWareHouse.SelectedIndex = 0;
        }

        private void FrmStockSearch_Load(object sender, EventArgs e)
        {
            InitDictItem();

            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;
        }

        private void txtSearchWareHouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = txtSearchWareHouse.SelectedItem as CListItem;
            this.txtGoodsType.Items.Clear();
            if (item != null && !string.IsNullOrEmpty(item.Value))
            {
                if (item.Value == "客房仓库")
                {
                    List<GoodsTypeInfo> list = BLLFactory<GoodsType>.Instance.GetAll();
                    foreach (GoodsTypeInfo info in list)
                    {
                        this.txtGoodsType.Items.Add(new CListItem(info.GoodsType));
                    }
                }
                else if (item.Value == "KTV仓库")
                {
                    List<KTVGoodsTypeInfo> list = BLLFactory<KTVGoodsType>.Instance.GetAll();
                    foreach (KTVGoodsTypeInfo info in list)
                    {
                        this.txtGoodsType.Items.Add(new CListItem(info.GoodsType));
                    }
                }
                else if (item.Value == "茶室仓库")
                {
                    List<TeaGoodsTypeInfo> list = BLLFactory<TeaGoodsType>.Instance.GetAll();
                    foreach (TeaGoodsTypeInfo info in list)
                    {
                        this.txtGoodsType.Items.Add(new CListItem(info.GoodsType));
                    }
                }
            }
            this.txtGoodsType.Items.Insert(0, new CListItem("所有项目", ""));
            BindData();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void menu_InRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            //ItemNo,Name,UnitName,StockQuantity,AlarmQuantity,UnitCost,StockAmount, Price, Amount
            #region 添加别名解析
            this.winGridView1.AddColumnAlias("ItemNo", "项目编号");
            this.winGridView1.AddColumnAlias("Name", "项目名称");
            this.winGridView1.AddColumnAlias("UnitName", "单位");
            this.winGridView1.AddColumnAlias("StockQuantity", "当前库存");
            this.winGridView1.AddColumnAlias("AlarmQuantity", "报警库存");
            this.winGridView1.AddColumnAlias("UnitCost", "单位成本");
            this.winGridView1.AddColumnAlias("StockAmount", "库存总额");
            this.winGridView1.AddColumnAlias("Price", "预设单价");
            this.winGridView1.AddColumnAlias("Amount", "预售总额");
            this.winGridView1.AddColumnAlias("WareHouse", "仓库名称");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("GoodsType", this.txtGoodsType.Text.Replace("所有项目", ""), SqlOperator.Like);
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            DataTable dt = new DataTable();
            #region 获取报表内容
            CListItem item = this.txtSearchWareHouse.SelectedItem as CListItem;
            if (item == null)
                return;
            string wareHouse = item.Value;

            if (wareHouse == "客房仓库")
            {
                dt = BLLFactory<Goods>.Instance.GetCurrentStockReport(filter);
            }
            else if (wareHouse == "KTV仓库")
            {
                dt = BLLFactory<KTVGoods>.Instance.GetCurrentStockReport(filter);
            }
            else if (wareHouse == "茶室仓库")
            {
                dt = BLLFactory<TeaGoods>.Instance.GetCurrentStockReport(filter);
            }
            else
            {
                dt = BLLFactory<Goods>.Instance.GetAllCurrentStockReport(filter);
            } 
            #endregion

            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                int StockQuantity = 0;
                decimal StockAmount = 0M;
                decimal Amount = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    StockQuantity += Convert.ToInt32(row["StockQuantity"]);
                    StockAmount += Convert.ToDecimal(row["StockAmount"]);
                    Amount += Convert.ToDecimal(row["Amount"]);
                }
                dr["StockQuantity"] = StockQuantity;
                dr["StockAmount"] = StockAmount;
                dr["Amount"] = Amount;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            }
            #endregion
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "当前库存查询统计报表";
 
        }

        private void txtGoodsType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindData();
            }
        }
    }
}
